/*
 * @Date: 2021-08-18 09:43:33
 * @information:
 */
import LikeDiaryModel from '../model/LikeDiaryModel'


export default class LikeDiaryDao {
  async findByDiaryId (data) {
    const { diaryId } = data
    return await LikeDiaryModel.findAll({
      attributes: { exclude: ['isdelete'] }, // 返回值排除字段
      order: [ // 排序
        ['createtime', 'DESC']
      ],
      where: {
        isdelete: 0,
        diaryId,
      },
    })
  }

  async findByDiaryIdAndFromUserIdAndToUserId (data) {
    const { diaryId, toUserId, fromUserId } = data
    return await LikeDiaryModel.findOne({
      where: {
        diaryId,
        toUserId,
        fromUserId,
      },
    })
  }

  async getCountByDiaryId (data) {
    const { diaryId } = data
    return await LikeDiaryModel.count({
      where: {
        isdelete: 0,
        diaryId,
      }
    })
  }

  async add (data) {
    return await LikeDiaryModel.create(data)
  }

  async del (data) {
    const { id } = data
    return await LikeDiaryModel.update({ isdelete: 1 }, {
      where: {
        isdelete: 0,
        id,
      }
    })
  }

  async recover (data) {
    const { id } = data
    const obj = {
      isdelete: 0,
      updatetime: Date.now(),
    }
    return await LikeDiaryModel.update(obj, {
      where: {
        isdelete: 1,
        id,
      }
    })
  }


}
